package com.itheima.reggie.builder;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.pojo.*;
import com.itheima.reggie.service.*;
import com.itheima.reggie.service.impl.OrdersServiceImpl;
import org.springframework.beans.BeanUtils;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

public class FullOrderDataBuilder {
    private FullOrderData fullOrderData;
    private ShoppingCartService shoppingCartService;
    private UserService userService;
    private AddressBookService addressBookService;
    private Orders orders;
    AtomicInteger amount = new AtomicInteger(0);

    private Long userId;
    public FullOrderDataBuilder( Orders orders, ShoppingCartService shoppingCartService, UserService userService, AddressBookService addressBookService) {
        this.shoppingCartService = shoppingCartService;
        this.orders = orders;
        this.userService = userService;
        this.fullOrderData=new FullOrderData();
        this.addressBookService=addressBookService;
        this.userId=orders.getUserId();
    }
    public FullOrderDataBuilder buildOrders() {
        Long addressBookId = orders.getAddressBookId();
        //查询地址
        AddressBook addressBook = addressBookService.getById(addressBookId);
        if (addressBook == null) {
            throw new CustomException("用户地址有误，无法下单");
        }
        User user = userService.getById(orders.getUserId());
        orders.setOrderTime(LocalDateTime.now());
        orders.setCheckoutTime(LocalDateTime.now());
        orders.setStatus(2);
        orders.setAmount(new BigDecimal(amount.intValue()));//总金额
        orders.setUserId(userId);
        orders.setNumber(String.valueOf(orders.getId()));
        orders.setUserName(user.getName());
        orders.setConsignee(addressBook.getConsignee());
        orders.setPhone(addressBook.getPhone());
        orders.setAddress((addressBook.getProvinceName() == null ? "" : addressBook.getProvinceName())
                + (addressBook.getCityName() == null ? "" : addressBook.getCityName())
                + (addressBook.getDistrictName() == null ? "" : addressBook.getDistrictName())
                + (addressBook.getDetail() == null ? "" : addressBook.getDetail()));
        fullOrderData.setOrders(orders);
        return this;
    }
    public FullOrderDataBuilder buildOrderDetails() {
        List<ShoppingCart> shoppingCarts = shoppingCartService.list(new QueryWrapper<ShoppingCart>().eq("user_id", userId));
        //插入订单明细表  多条数据
        List<OrderDetail> collect = shoppingCarts.stream().map(shoppingCart -> {
            OrderDetail orderDetail = new OrderDetail();
            BeanUtils.copyProperties(shoppingCart, orderDetail, "id");//除了id
            orderDetail.setOrderId(orders.getId());
            BigDecimal multiply = orderDetail.getAmount().multiply(new BigDecimal(orderDetail.getNumber()));
            amount.addAndGet(multiply.intValue());
            return orderDetail;
        }).collect(Collectors.toList());
        fullOrderData.setOrderDetails(collect);
        return this;
    }
    public FullOrderData build() {
        return fullOrderData;
    }


}
